/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gestionmagasins_avec_patterns.forms;

import gestionmagasins_avec_patterns.model.Usager;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;

/**
 *
 * @author James
 */
public class CustomJFrame extends javax.swing.JFrame {

    protected Usager usager;
    protected JFrame parentJFrame;
    
    
    public CustomJFrame(JFrame parentJFrame, Usager usager)
    {
        initComponents();
        
        this.setLocationRelativeTo(null);
        
        this.parentJFrame = parentJFrame;
        this.usager = usager;  
        
        setTitle("Super Logiciel inc. AVEC Patterns");
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        formClosing();
    }//GEN-LAST:event_formWindowClosing

    protected void formClosing()
    {
        this.setVisible(false);
        this.parentJFrame.setVisible(true);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    protected KeyListener k = new KeyListener()
    {
        @Override
        public void keyReleased(KeyEvent arg0){}

        @Override
        public void keyPressed(KeyEvent evt)
        {
            if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
                CustomJFrame.this.dispatchEvent(new WindowEvent(CustomJFrame.this, WindowEvent.WINDOW_CLOSING));
        }

        @Override
        public void keyTyped(KeyEvent e) {}
    };
    
    protected void initKeyListeners()
    {
        for(Component c : getAllComponents(getRootPane()))
            c.addKeyListener(k);
    }
    
    public static List<Component> getAllComponents(final Container c) 
    {
        Component[] comps = c.getComponents();
        List<Component> compList = new ArrayList<Component>();
        
        for (Component comp : comps) 
        {
            compList.add(comp);
            
            if (comp instanceof Container)
                compList.addAll(getAllComponents((Container) comp));
        }
        return compList;
    }

}
